﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_BUS;
using HotelSoft.HotelManagement_DTO;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmTra_PHONG : Form
    {
        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }

        Double temp = 1;
        PhieuDangKyBUS busPhieuDangKy = new PhieuDangKyBUS();
        KhachHang_PhieuDangKyBUS busKhachHang_PhieuDangKy = new KhachHang_PhieuDangKyBUS();
        KhachHangDTO dtoKhachHang= new KhachHangDTO();
        KhachHangBUS busKhachHang = new KhachHangBUS();
        PhongBUS busPhong = new PhongBUS();
        frmQuanly_HOADONTHANHTOAN frHoaDonThanhToan;
        private static float TTien = 0;
        private static string Sopdk;
        private static string Sohd;
        private static string Sophong;
        private static string Ngaythue;
        private static string Songaythue;
        private static string SoPhieuDK;
        public string SOPHIEUDK
        {
            get { return SoPhieuDK; }
            set { SoPhieuDK = value; }
        }

        public frmTra_PHONG()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void frmTra_PHONG_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            txtGioiTinh.ReadOnly = true;
            txtSoPhong.ReadOnly = true;
            txtSoKhachToiDa.ReadOnly = true;
            txtTenKhachHang.ReadOnly = true;
            txtDiaChi.ReadOnly = true;
            txtQuocTich.ReadOnly = true;
            txtTongTien.ReadOnly = true;
            lblSophieuDK.Text = SoPhieuDK;
        }
        
        //public void LoadCombobox()
        //{
        //    DataTable tbl = new DataTable();
        //    tbl = busPhieuDangKy.SelectAll();
        //    cbbSoPhieuDangKy.DisplayMember = "SoPDK";
        //    cbbSoPhieuDangKy.ValueMember = "SoPDK";
        //    cbbSoPhieuDangKy.DataSource = tbl;
        //}
       
        //private void cbbSoPhieuDangKy_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    //if (cbbSoPhieuDangKy.SelectedIndex != -1)
        //    //{
        //    //    DataTable tbl = new DataTable();
        //    //    PhieuDangKyDTO dto = new PhieuDangKyDTO();
        //    //    dto.SoPDK = cbbSoPhieuDangKy.SelectedValue.ToString();
        //    //    tbl = busPhieuDangKy.SelectphieudangkytheoSoPDK(dto);
        //    //    string st1 = tbl.Rows[0].ItemArray[6].ToString();
        //    //    string st2 = tbl.Rows[0].ItemArray[1].ToString();
        //    //    txtSoPhong.Text = st1.ToString();
        //    //    Sophong = st1.ToString();
        //    //    txtSoKhachToiDa.Text = st2.ToString();
        //    //    LoadCombobox1();
        //    //    LoadCombobox2();
        //    //}
        //}

        public void LoadCombobox1()
        {
            DataTable tbl1 = new DataTable();
            KhachHang_PhieuDangKyDTO dto3= new KhachHang_PhieuDangKyDTO();

            dto3.SoPDK = lblSophieuDK.Text;
            tbl1 = busKhachHang_PhieuDangKy.SelectKhachHang_PhieuDangKytheoSoPDK(dto3);
            cbbCMND.DisplayMember = "CMND";
            cbbCMND.ValueMember = "CMND";
            cbbCMND.DataSource = tbl1;
        }

        private void cbbCMND_SelectedIndexChanged(object sender, EventArgs e)
       {
            if (cbbCMND.SelectedIndex != -1)
            {
                DataTable tbl = new DataTable();
                KhachHangDTO dto = new KhachHangDTO();
                dto.CMND = cbbCMND.SelectedValue.ToString();
                tbl = busKhachHang.Selectkhachhangtheocmn(dto);
                string st1 = tbl.Rows[0].ItemArray[1].ToString();
                string st2 = tbl.Rows[0].ItemArray[4].ToString();
                string st3 = tbl.Rows[0].ItemArray[5].ToString();
                txtTenKhachHang.Text = st1.ToString();
                txtDiaChi.Text = st2.ToString();
                txtQuocTich.Text = st3.ToString();
                txtGioiTinh.Text = tbl.Rows[0].ItemArray[3].ToString();
            }
        }

        public void LoadCombobox2()
        {
            DataTable tbl1 = new DataTable();
            KhachHang_PhieuDangKyDTO dto = new KhachHang_PhieuDangKyDTO();
            dto.SoPDK = lblSophieuDK.Text;
            tbl1 = busKhachHang_PhieuDangKy.SelectKhachHang_PhieuDangKytheoSoPDKtongtien(dto);
            Decimal tongtien=0;
            tongtien=Decimal.Parse(tbl1.Rows[0].ItemArray[0].ToString());
            TTien = float.Parse(tbl1.Rows[0].ItemArray[0].ToString());
            Sopdk = lblSophieuDK.Text;
            Sohd = lblSophieuDK.Text;
            txtTongTien.Text = tongtien.ToString("#,##0 VNĐ");
        }

        private void btnLapHoaDonThanhToan_Click(object sender, EventArgs e)
        {
            frHoaDonThanhToan = new frmQuanly_HOADONTHANHTOAN();
            frHoaDonThanhToan.TONGTIEN = TTien;
            frHoaDonThanhToan.SOpdk = Sopdk;
            frHoaDonThanhToan.SOhd = Sohd;
            frHoaDonThanhToan.NGAYTHUE = lblNgaythue.Text;
            frHoaDonThanhToan.SONGAYTHUE = Songaythue;
            this.Hide();
            frHoaDonThanhToan.Show();
            frHoaDonThanhToan.FormClosing += new FormClosingEventHandler(frHoaDonThanhToan_FormClosing);
        }

        void frHoaDonThanhToan_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
            LoadCombobox2();
        }

        private void btnTrangThai_Click(object sender, EventArgs e)
        {
            HoaDon_ThanhToanDTO dto = new HoaDon_ThanhToanDTO();
            HoaDon_ThanhToanBUS busHD=new HoaDon_ThanhToanBUS();
            dto.SoPDK = lblSophieuDK.Text;
            if (!busHD.KTHD(dto))
            {
                MessageBox.Show("Số phiếu đăng ký này chưa có hoá đơn thanh toán\nXin hãy lập hoá đơn trước!","Thông báo trả phòng");
                btnLapHoaDonThanhToan.Focus();
                return;
            }

            PhongDTO dtoPH = new PhongDTO();
            dtoPH.SOPHONG = txtSoPhong.Text;
            PhongBUS busPhong = new PhongBUS();
            if (busPhong.tablePhongdondulieu(dtoPH))
                MessageBox.Show("Cập nhật tình trạng phòng thành công!");
            btnThoat.Focus();
            btnTrangThai.Enabled = false;
        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lblSophieuDK_TextChanged(object sender, EventArgs e)
        {
            DataTable tbl = new DataTable();
            PhieuDangKyDTO dto = new PhieuDangKyDTO();
            dto.SoPDK = lblSophieuDK.Text;
            tbl = busPhieuDangKy.SelectphieudangkytheoSoPDK(dto);
            string st1 = tbl.Rows[0].ItemArray[6].ToString();
            string st2 = tbl.Rows[0].ItemArray[1].ToString();
            string st3 = tbl.Rows[0].ItemArray[2].ToString();
            string st4 = tbl.Rows[0].ItemArray[3].ToString();
            Songaythue = st4;
            lblNgaythue.Text = st3;
            Ngaythue = st3;
            txtSoPhong.Text = st1.ToString();
            Sophong = st1.ToString();
            txtSoKhachToiDa.Text = st2.ToString();
            LoadCombobox1();
            LoadCombobox2();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }
    }
}
